<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" title="库存信息" width="1000px">
    <AdTable @register="registerTable" size="small" @row-db-click="handleSelect">
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :stop-button-propagation="true"
            :actions="[
              {
                icon: 'line-md:confirm-circle',
                auth: '', // 权限控制
                tooltip: '选择',
                ifShow: (_action) => {
                  return true;
                },
                onClick: handleSelect.bind(null, record),
              },
            ]"
          />
        </template>
      </template>
    </AdTable>
  </BasicDrawer>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { useI18n } from '@/hooks/web/useI18n';
  import { isFunction } from '@/utils/is';
  import { propTypes } from '@/utils/propTypes';
  import { BasicDrawer, useDrawerInner } from '@/components/Drawer';
  import { AdColumn, AdTable, useAdTable, TableAction } from '@/components/AdTable';
  import { FilterRule, FilterGroup } from '@/utils/common/types';
  import Icon from '@/components/Icon/Icon.vue';
  import { FilterOperateEnum } from '@/enums/filterOperateEnum';
  import { Read } from '../material/material.api';
  import { columns } from '../material/material.data';

  export default defineComponent({
    name: 'MaterialSelectDrawer',
    components: { BasicDrawer, AdTable, TableAction, Icon },
    props: {
      callBackFunc: {
        type: Function as PropType<(record?: Recordable) => void>,
        default: null,
      },
    },
    setup(props) {
      const { t } = useI18n();
      const filterGroupRef = ref<FilterGroup>(new FilterGroup());

      const [registerTable, { reload }] = useAdTable({
        // 表标题呈现器
        title: '库存列表',
        // 表格数据接口请求对象
        api: Read,
        // 表单列信息 BasicColumn[]
        columns: columns,
        // 是否可拖拽列
        canColDrag: false,
        // 使用搜索表单
        useSearchForm: true,
        // 显示表格设置工具
        showTableSetting: true,
        //表格设置项
        tableSetting: { redo: true, size: true, setting: false, fullScreen: false },
        // 是否显示表格边框
        bordered: false,
        // 是否显示序号列
        showIndexColumn: true,
        clickToRowSelect: true,
        rowKey: 'Id', //行Key
        rowSelection: { type: 'radio', preserveSelectedRowKeys: true }, // 行选中类型 radio | checkbox
        // 表格右侧操作列配置 BasicColumn
        actionColumn: {
          width: 125,
          title: '操作',
          dataIndex: 'action',
          fixed: 'right',
        },
      });

      const [registerDrawer, drawerMethods] = useDrawerInner(async (data) => {
        console.log(data);
        drawerMethods.setDrawerProps({ confirmLoading: false, zIndex: 99999999 });
      });

      const handleSelect = async function handleSelect(record: Recordable) {
        try {
          if (props.callBackFunc && isFunction(props.callBackFunc)) {
            props.callBackFunc(record);
          }
        } finally {
          drawerMethods.closeDrawer();
        }
      };

      return {
        t,
        registerDrawer,
        registerTable,
        filterGroupRef,
        handleSelect,
      };
    },
  });
</script>
